#extension GL_EXT_gpu_shader4 : enable

#define	 Pi	3.14159265

uniform isampler2D itemBufferSampler;

uniform vec3 shootPosition;
uniform vec3 shootDirectionVector;
uniform vec3 receiverNormal;

varying vec4 itemBufferTexCoords;
varying vec3 receiverPosition;
uniform int faceId;

float FormFactor(float shooterDeltaArea)
{
	float visible = float(texture2DProj(itemBufferSampler, itemBufferTexCoords).x == faceId);

	vec3 R = shootPosition - receiverPosition;
	float distanceSquared = dot(R, R);
	R = R * inversesqrt(distanceSquared);
	float cosi = dot(receiverNormal, R);
	float cosj = -dot(shootDirectionVector, R);
	float equationTop = max(cosi * cosj, 0.0);
	float equationBottom = (Pi * distanceSquared) + shooterDeltaArea;
	return (visible * (equationTop / equationBottom));
}
